<template>
  <div>
    <ColumnList :dataList="dataList" @operation="handles"></ColumnList>
    <ColumnModal :showBtn="false" ref="columnModalRef"></ColumnModal>
  </div>
</template>
<script setup lang="ts">
  // GET /u/authology
  import {SUCCESS_CODE} from "~/common/constants";

  const nuxtApp = useNuxtApp()

  const dataList = ref([])
  const editCollectData = ref(null)
  const columnModalRef = ref(null)
  async function getAuthologyList(){
    const {data, msg} = await nuxtApp.$request('/u/authology',{
      method: 'GET',
      server: true,
      key: 'getAuthologyList'
    }).catch(e => {
      // nuxtApp.$mutual['notification'].error({
      //   content: e.message,
      //   duration: 5000
      // })
    })
    dataList.value = data || []
  }
  getAuthologyList()
  function handFn(id:string, type: string) {
    // DELETE /u/authology/{id:文集id}
    const isDelete = type === 'delete'
    nuxtApp.$mutual['dialog'].warning({
      title: '提示',
      content: `确定${isDelete ? '删除' : '置顶'}？`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const url = isDelete ? `/u/authology/${id}` : `/u/authology/${id}?sticky=true`
        const {data, code, msg} = await nuxtApp.$request(url,{
          method: type === 'delete' ? 'DELETE' : 'POST',
          server: true,
          key: 'deleteAuthologyById'
        }).catch(e => {
          // nuxtApp.$mutual['notification'].error({
          //   content: e.message,
          //   duration: 5000
          // })
        })
        if (code === SUCCESS_CODE) {
          nuxtApp.$mutual['notification'].success({
            content: msg,
            duration: 3000
          })
          getAuthologyList()
        }
      },
      onNegativeClick: () => {
      }
    })
  }
  function handles(e) {
    const {type, id} = e
    if (type === 'delete') {
      handFn(id, type)
    } else if (type === 'top') {
      handFn(id, type)
    } else if (type === 'edit') {
      // editCollectData.value = {...id}
      console.log(dataList.value.find(item => item.id === id ))
      columnModalRef.value.modalOpen(dataList.value.find(item => item.id === id ))
    } else if (type === 'management') {

    }
  }
</script>

<style scoped lang="scss">

</style>
